﻿/*
 * ======================================================================
 *                           Modern Warfare3 Launcher
 * Copyright 2011-2012
 * Developer:Pooya parsa
 * Email:pingsft@gmail.com
 * You are ALLOWED:
 * -Read source code
 * -Use some part of source code with Full name and email of developer
 * -Release your own mod with name of develepor
 * You are NOT ALLOWD:
 * -Add cracked version of Mw3
 * -Release modified version as MW3Launcher
 * -Mirror Sourcecode
 * -Use source code without name of developer
 * Thank you for your attention
 * ======================================================================
 * */
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;

namespace MW3Launcher
{
    public class IniParser
    {
        private readonly string _iniFilePath;
        private readonly Hashtable _keyPairs = new Hashtable();

        public IniParser(string iniPath)
        {
            TextReader reader = null;
            string str = null;
            string str2 = null;
            string[] strArray = null;
            this._iniFilePath = iniPath;
            if (File.Exists(iniPath))
            {
                try
                {
                    try
                    {
                        reader = new StreamReader(iniPath);
                        for (str = reader.ReadLine(); str != null; str = reader.ReadLine())
                        {
                            str = str.Trim();
                            if (str != "")
                            {
                                if (str.StartsWith("[") && str.EndsWith("]"))
                                {
                                    str2 = str.Substring(1, str.Length - 2);
                                }
                                else
                                {
                                    SectionPair pair;
                                    strArray = str.Split(new char[] { '=' }, 2);
                                    string str3 = null;
                                    if (str2 == null)
                                    {
                                        str2 = "ROOT";
                                    }
                                    pair.Section = str2;
                                    pair.Key = strArray[0];
                                    if (strArray.Length > 1)
                                    {
                                        str3 = strArray[1];
                                    }
                                    this._keyPairs.Add(pair, str3);
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        throw exception;
                    }
                    return;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
            throw new FileNotFoundException("Unable to locate " + iniPath);
        }

        public void AddSetting(string sectionName, string settingName)
        {
            this.AddSetting(sectionName, settingName, null);
        }

        public void AddSetting(string sectionName, string settingName, string settingValue)
        {
            SectionPair pair;
            pair.Section = sectionName;
            pair.Key = settingName;
            if (this._keyPairs.ContainsKey(pair))
            {
                this._keyPairs.Remove(pair);
            }
            this._keyPairs.Add(pair, settingValue);
        }

        public void DeleteSetting(string sectionName, string settingName)
        {
            SectionPair pair;
            pair.Section = sectionName;
            pair.Key = settingName;
            if (this._keyPairs.ContainsKey(pair))
            {
                this._keyPairs.Remove(pair);
            }
        }

        public string[] EnumSection(string sectionName)
        {
            ArrayList list = new ArrayList();
            foreach (SectionPair pair in this._keyPairs.Keys)
            {
                if (pair.Section == sectionName)
                {
                    list.Add(pair.Key);
                }
            }
            return (string[])list.ToArray(typeof(string));
        }

        public string GetSetting(string sectionName, string settingName)
        {
            try
            {
                SectionPair pair;
                pair.Section = sectionName;
                pair.Key = settingName;
                return (string)this._keyPairs[pair];
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        

        public void SaveSettings()
        {
            this.SaveSettings(this._iniFilePath);
        }

        public void SaveSettings(string newFilePath)
        {
            ArrayList list = new ArrayList();
            string str = "";
            string str2 = "";
            foreach (SectionPair pair in this._keyPairs.Keys)
            {
                if (!list.Contains(pair.Section))
                {
                    list.Add(pair.Section);
                }
            }
            foreach (string str3 in list)
            {
                str2 = str2 + "[" + str3 + "]\r\n";
                foreach (SectionPair pair2 in this._keyPairs.Keys)
                {
                    if (pair2.Section == str3)
                    {
                        str = (string)this._keyPairs[pair2];
                        if (str != null)
                        {
                            str = "=" + str;
                        }
                        str2 = str2 + pair2.Key + str + "\r\n";
                    }
                }
                str2 = str2 + "\r\n";
            }
            try
            {
                TextWriter writer = new StreamWriter(newFilePath);
                writer.Write(str2);
                writer.Close();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SectionPair
        {
            public string Section;
            public string Key;
        }
    }
}
